Enegizing change

df_seasonality <- read.csv("../data/df_seasonality.csv")
df_swisspop_2022 <- read.csv("../data/df_swisspop_2022.csv")
map_data_sf <- readRDS("../data/map_data_sf.rds")
df_ch <- read.csv("../data/swiss_specific_fuel.csv")
df_fr <- read.csv("../data/french_specific_fuel.csv")
charge_ch_fr <- read.csv("../data/df_charging_points_CH_FR.csv")
df_v_fr <- read.csv("../data/df_v_fr.csv")
df_v_ch <- read.csv("../data/df_v_ch.csv")
df_corr <- read.csv("../data/df_corr.csv")

RQ1

Car Registrations Over Time


Trend over Time

# Ensure the YearMonth is in Date format
df_seasonality$YearMonth <- as.Date(df_seasonality$YearMonth, format = "%Y-%m-%d")

# Calculate a smoothed series using a rolling mean or LOESS
# For a rolling mean:
df_seasonality$Smoothed <- rollmean(df_seasonality$Count, k = 12, fill = NA)

# Alternatively, for LOESS smoothing:
# loess_fit <- loess(Count ~ as.numeric(YearMonth), data = df_seasonality, span = 0.5)
# df_seasonality$Smoothed <- predict(loess_fit)

# Create an xts object with both the original and smoothed counts
df_xts <- xts(df_seasonality[, c("Count", "Smoothed")], order.by = df_seasonality$YearMonth)

# Plot using dygraphs
dygraph_object <- dygraph(df_xts, main = "Passenger Car Adoption Over Time in Switzerland") %>%
  dySeries("Count", label = "Number of Passenger Cars Registered") %>%
  dySeries("Smoothed", label = "Smoothed Trend") %>%
  dyOptions(stackedGraph = FALSE) %>%
  dyRangeSelector(height = 20)

# Print the dygraph to display it
dygraph_object

Monthly Trend

p_seaso_2 <- ggplot(df_seasonality, aes(x = Month, y = Count, group = Year, color = as.factor(Year))) +
  geom_smooth(se = FALSE, method = "loess", span = 0.5, size = 0.7) +
  labs(title = "Monthly Passenger Car Registrations by Year",
       x = "Month",
       y = "Number of Passenger Cars Registered",
       color = "Year") +
  theme_minimal() +
  scale_color_viridis_d() +
  theme(legend.position = "bottom", axis.text.x = element_text(angle = 45, hjust = 1))
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
# Convert to an interactive plotly object
interactive_plot_seaso_2 <- ggplotly(p_seaso_2)
`geom_smooth()` using formula = 'y ~ x'
# Adjust plotly settings if needed, such as margins or layout
interactive_plot_seaso_2 <- interactive_plot_seaso_2 %>%
  layout(margin = list(l = 10, r = 10, b = 40, t = 40), # Adjust margins
         legend = list(orientation = "h", x = 0, xanchor = "left", y = -0.2)) # Adjust legend position

# Display the interactive plot
(interactive_plot_seaso_2)

Monthly Trend 2

df_seasonality$Year <- as.factor(df_seasonality$Year)
p_seaso_3 <- ggplot(df_seasonality, aes(x = Month, y = Count, group = Year, color = Year)) +
  geom_line() +
  facet_wrap(~ Year, scales = "free_y") +
  labs(title = "Seasonal Trends in Passenger Car Registrations",
       x = "Month",
       y = "Number of Passenger Cars Registered") +
  theme_minimal() +
  scale_color_viridis_d(guide = FALSE) +
  theme(axis.text.x = element_blank(),  # Combine axis.text.x settings here
        axis.text.y = element_blank(),
        axis.ticks.x = element_blank(),
        legend.position = "none")

interactive_plot_seaso_3 <- ggplotly(p_seaso_3) %>%
  layout(xaxis = list(tickmode = "array",
                      tickvals = 1:12,
                      ticktext = month.abb))

interactive_plot_seaso_3

EV Registration

detach("package:xts", unload = TRUE)
# Create color palettes for the 'Total' and 'EV_per_Capita' columns
color_palette_total <- colorNumeric(palette = "viridis", domain = map_data_sf$TotalEV)

leaflet(map_data_sf) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    fillColor = ~color_palette_total(TotalEV),
    weight = 1,
    color = "#FFFFFF",
    fillOpacity = 0.7,
    popup = ~paste(NAME, "<br>Total EV Registrations: ", TotalEV)
  ) %>%
  addLegend(
    pal = color_palette_total, 
    values = ~TotalEV, 
    opacity = 0.7, 
    title = "Total EV <br> Registrations",
    position = "topright"
  )

EV Registration Standardized

color_palette_per_capita <- colorNumeric(palette = "viridis", domain = map_data_sf$EV_per_Capita)

leaflet_map_per_capita <- leaflet(map_data_sf) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    fillColor = ~color_palette_per_capita(EV_per_Capita),
    weight = 1,
    color = "#FFFFFF",
    fillOpacity = 0.7,
    popup = ~paste(NAME, "<br>EV Registrations per Capita: ", 
                   round(EV_per_Capita, 3))
  ) %>%
  addLegend(
    pal = color_palette_per_capita, 
    values = ~EV_per_Capita, 
    opacity = 0.7, 
    title = "EV Registrations <br> per Capita",
    position = "topright"
  )
leaflet_map_per_capita

Car Registrations Over Time

Swiss Registration Dataset

# Assuming you have a data frame named df_seasonality
datatable(df_seasonality[, !names(df_seasonality) %in% c("Smoothed", "YearMonth")], options = list(pageLength = 10, autoWidth = TRUE))

Swiss Population

# Assuming you have a data frame named df_seasonality
datatable(df_swisspop_2022[, !names(df_swisspop_2022) %in% c("CantonAbbreviation", "KANTONSNUM")], options = list(pageLength = 10, autoWidth = TRUE))

RQ2

Correlation of EV with Demographics

# Creating the heatmap
p <- ggplot(df_corr, aes(Var1, Var2, fill = value)) +
  geom_tile() +
  geom_text(aes(label = sprintf("%.2f", value)), color = "white", size = 4) +
  scale_fill_gradient(low = "lightblue", high = "darkblue", name = "Correlation") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(x = "", y = "", title = "Correlation Heatmap")

# Convert to interactive plot
ggplotly(p, tooltip = c(sprintf("value")))

RQ3

Swiss vs France


Electric Trend over Time

# Assuming df_ch and df_fr are already loaded and the 'Date' columns are of type character
df_ch$Date <- as.Date(df_ch$Date)
df_fr$Date <- as.Date(df_fr$Date)

# Define color palette for each fuel type
fuel_colors <- c(
  "Diesel" = viridis(5)[1], 
  "Electricity" = viridis(5)[2], 
  "Conventional hybrid" = viridis(5)[3], 
  "Plug-in hybrid" = viridis(5)[4], 
  "Petrol" = viridis(5)[5]
)
# Create the ggplot
p <- ggplot() +
  geom_line(data = df_fr, aes(x = Date, y = Count, color = Fuel), size = 1) +
  geom_line(data = df_ch, aes(x = Date, y = Count, color = Fuel), size = 1) +
  scale_color_manual(values = fuel_colors) +
  labs(x = "Date", y = "Standardized Count", color = "Fuel Type") +
  theme_minimal() +
  scale_x_date() # This will handle the dates on the x-axis

# Convert to interactive plot using plotly
interactive_plot <- ggplotly(p, tooltip = c("x", "y", "color"))

# Use style() to set the visibility of the lines
for (i in 1:length(interactive_plot$x$data)) {
  # Assuming the name of the trace includes the fuel type
  if (grepl("Electricity", interactive_plot$x$data[[i]]$name)) {
    interactive_plot$x$data[[i]]$visible <- TRUE
  } else {
    interactive_plot$x$data[[i]]$visible <- 'legendonly'
  }
}

# Adjust the layout of the interactive plot, if needed
interactive_plot <- interactive_plot %>%
  layout(legend = list(orientation = "h", x = 0.5, xanchor = "center", y = -0.2)) # Adjust the legend position

# Print the plot
interactive_plot

Chargin Station

data <- charge_ch_fr 

# Convert year to Date format and then extract the year
data$year <- as.Date(paste0(data$year, "-01-01"))
data$year <- format(data$year, "%Y")

# Sum the values by year and region
data_summarized <- data %>%
  group_by(year, region) %>%
  summarize(total_value = sum(value))
`summarise()` has grouped output by 'year'. You can override using the
`.groups` argument.
# Create the ggplot
p <- ggplot(data_summarized, aes(x = year, y = total_value, fill = region)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_fill_manual(values = c("France" = "#4E79A7", "Switzerland" = "#F28E2B")) +  # Adjusted custom colors
  labs(title = "Total Availability of Charging Stations in France vs Switzerland",
       x = "Year",
       y = "Total Charging Stations") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5), # Center the plot title
        legend.title = element_blank()) # Remove the legend title

# Convert to interactive plot using plotly
p_interactive <- ggplotly(p)
p_interactive <- ggplotly(p, tooltip = c("x", "y", "color"))
p_interactive <- p_interactive %>%
  layout(legend = list(orientation = "h", x = 0, xanchor = "left", y = -0.2))
p_interactive

Swiss Vehicle Dataset

Swiss Vehicle Dataset

datatable(df_v_ch[, !names(df_v_ch) %in% c("Location", "VehicleType", "X")], options = list(pageLength = 10, autoWidth = TRUE))

French Vehicle Dataset

datatable(df_v_fr[, !names(df_v_fr) %in% c("X", "Diesel", "Essence", "Convetional_Hybrid", "Plug_in_Hybrid", "Electrique")], options = list(pageLength = 10, autoWidth = TRUE))

Chargin Station Dataset

datatable(charge_ch_fr[, !names(charge_ch_fr) %in% c("X", "Diesel", "Essence", "Convetional_Hybrid", "Plug_in_Hybrid", "Electrique")], options = list(pageLength = 10, autoWidth = TRUE))